package com.bingqing.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.bingqing.common.AutoLog;
import com.bingqing.common.Result;
import com.bingqing.entity.Admin;
import com.bingqing.entity.Audit;
import com.bingqing.entity.Book;
import com.bingqing.entity.Params;
import com.bingqing.service.AdminService;
import com.bingqing.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: AdminController
 * Package: com.bingqing.springboot.controller
 * Description:
 *
 * @Author liuyujie
 * @Create 2024-03-06 13:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/book")
@CrossOrigin //解决跨域问题
public class BookController {
    @Resource
    private BookService bookService;

    //分页查询
    @GetMapping("/search")
    public Result findBySearch(Params params) {
        PageInfo<Book> info = bookService.findBySearch(params);
        return Result.success(info);
    }

    @PostMapping("/add")
    @AutoLog("新增请假")
    public Result add(@RequestBody Book book){
        bookService.add(book);
        return Result.success();
    }
    @PostMapping("/edit")
    @AutoLog("编辑请假")
    public Result edit(@RequestBody Book book){
        bookService.update(book);
        return Result.success();
    }
    //删除
    @AutoLog("删除图书")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        bookService.deleteById(id);
        return Result.success();
    }
    //echarts/bar
    @GetMapping("/echarts/bar")
    public Result bar(){
        //1.查询出所有图书
        List<Book> list = bookService.findAll();
        System.out.println("list:"+list.toString());
        Map<String, Long> collect = list.stream()
                .filter(book -> ObjectUtil.isNotEmpty(book.getTypeName()))
                .collect(Collectors.groupingBy(Book::getTypeName, Collectors.counting()));
        List<String> xList = new ArrayList<>();
        List<Long> yList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
               xList.add(key);
               yList.add(collect.get(key));
            }

        }
        Map<String,Object> mapList = new HashMap<>();
        mapList.put("xAxis",xList);
        mapList.put("yAxis",yList);
        return Result.success(mapList);
    }

    @GetMapping("/echarts/bie")
    public Result bie(){
        //1.查询出所有图书
        List<Book> list = bookService.findAll();
        System.out.println("list:"+list.toString());
        Map<String, Long> collect = list.stream()
                .filter(book -> ObjectUtil.isNotEmpty(book.getTypeName()))
                .collect(Collectors.groupingBy(Book::getTypeName, Collectors.counting()));
        System.out.println("collect:"+collect.toString());
        List<Map<String,Object>> mapList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(collect)){
            for(String key: collect.keySet()){
                Map<String,Object> map = new HashMap<>();
                map.put("name",key);
                map.put("value",collect.get(key));
                mapList.add(map);
            }

        }
        return Result.success(mapList);

    }

}
